package com.ken.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ken.common.pojo.student;
import com.ken.common.pojo.user;


import java.util.Date;
import java.util.HashMap;

public class JWTUtil {
    private static final String SIGN = "!^&%&*!@$*%!!@(&%2ar^2t";
    //学生登录生成JWT令牌
    public static String createJWT(user user){
        HashMap<String, Object> map = new HashMap<>();
        String token = JWT.create()
                .withHeader(map) //设置头信息
                .withClaim("phone", user.getPhone()) //设置payload 携带信息
                .withClaim("id", user.getId())
                .withClaim("department", user.getPhone())
                .withClaim("name", user.getName())
                .withClaim("department",user.getDepartment())
                .withClaim("img",user.getImg())
                .withClaim("school",user.getSchool())
                .withClaim("role",user.getRole())//0为学生，1为老师
                .withExpiresAt(new Date(System.currentTimeMillis() + 10800 * 1000)) //设置失效时间
                .sign(Algorithm.HMAC256(SIGN)); //设置签名以及签名方式 这里使用HMAC256加密方式
        return token;
    }

    public static DecodedJWT ParseJWt(String jwt){
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(jwt);
    }
}
